home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / QuickDraw / Pallete DA / Pallet.c next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  5.3 KB  |  225 lines  |  [TEXT/MPS ]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        D/A shell.                                                            */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        D/A: Pallet                                                            */
  7. /*        Description: Displays the the current color table and palette        */
  8. /*                        information associated with it.                        */
  9. /*                                                                            */
  10. /*--------------------------------------------------------------------------*/
  11.  
  12. #include    <types.h>
  13. #include    <osutils.h>
  14. #include    <memory.h>
  15. #include    <devices.h>
  16. #include    <events.h>
  17. #include    <quickdraw.h>
  18. #include    <fonts.h>
  19. #include    <windows.h>
  20. #include    <files.h>
  21. #include    <errors.h>
  22. #include    <toolutils.h>
  23. #include    <packages.h>
  24. #include    <palettes.h>
  25.  
  26. /*------------------------------------------------------*/
  27. /*    Standard definitions.                                */
  28. /*------------------------------------------------------*/
  29.  
  30. #define    TRUE            0xFF
  31. #define    FALSE            0
  32.  
  33. /*------------------------------------------------------*/
  34. /*    Macros.                                                */
  35. /*------------------------------------------------------*/
  36.  
  37. #define OWNEDRSRCID(id)    (0xC000 | (((-(id)) - 1) << 5))
  38.  
  39. /*------------------------------------------------------*/
  40. /*    Program Specific definitions.                        */
  41. /*------------------------------------------------------*/
  42.  
  43. #define    APPHEAP        1
  44. #define    SYSHEAP        2
  45. #define    DISK        3
  46. #define    FREEON        4
  47.  
  48. #define    ACCEVENT    64
  49. #define    ACCRUN        65
  50.  
  51. /*------------------------------------------------------*/
  52. /*    Global Variables.                                    */
  53. /*------------------------------------------------------*/
  54.  
  55. /*------------------------------------------------------*/
  56. /*    Open.                                                */
  57. /*------------------------------------------------------*/
  58.  
  59. pascal short DRVROpen(CntrlParam *ctlPB, DCtlPtr dCtl)
  60. {
  61.     #pragma    unused (ctlPB)
  62.     GrafPtr            savePort;
  63.     WindowPeek        myWindow;
  64.     PaletteHandle    srcPalette;
  65.  
  66.     /*    If the windowPtr is non-nil, we already have a window open.  This desk accessory
  67.         ignores multiple opens.    */
  68.     if (dCtl->dCtlWindow != nil)
  69.         return noErr;
  70.  
  71.     GetPort(&savePort);
  72.         
  73.     myWindow = (WindowPeek) GetNewCWindow(OWNEDRSRCID(dCtl->dCtlRefNum), nil, (WindowPtr) -1);
  74.  
  75.     myWindow->windowKind = dCtl->dCtlRefNum;
  76.     dCtl->dCtlWindow = (WindowPtr) myWindow;
  77.  
  78.     srcPalette = NewPalette(256, nil, pmExplicit, 0);
  79.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  80.     ActivatePalette ((WindowPtr) myWindow);
  81.     SetPort(savePort);
  82.     return noErr;
  83. }
  84.  
  85. /*------------------------------------------------------*/
  86. /*    Prime.                                                */
  87. /*------------------------------------------------------*/
  88.  
  89. pascal short DRVRPrime(CntrlParam *ctlPB, DCtlPtr dCtl)
  90. {
  91.     #pragma    unused (ctlPB, dCtl)
  92.     return noErr;            /* Not used in this desk accessory */
  93. }
  94.  
  95. /*------------------------------------------------------*/
  96. /*    Status.                                                */
  97. /*------------------------------------------------------*/
  98.  
  99. pascal short DRVRStatus(CntrlParam *ctlPB, DCtlPtr dCtl)
  100. {
  101.     #pragma    unused (ctlPB, dCtl)
  102.     return noErr;            /* Not used in this desk accessory */
  103. }
  104.  
  105. /*------------------------------------------------------*/
  106. /*    Control.                                            */
  107. /*------------------------------------------------------*/
  108.  
  109. pascal short DRVRControl(CntrlParam *ctlPB, DCtlPtr dCtl)
  110. {
  111.     extern    void    doCtlEvent();
  112.     extern    void    doPeriodic();
  113.  
  114.     switch (ctlPB->csCode) {
  115.         case ACCEVENT:                /* accEvent */
  116.             doCtlEvent( *((EventRecord **) &ctlPB->csParam[0]));
  117.             break;
  118.  
  119.         case ACCRUN:                /* periodicEvent */
  120.             doPeriodic(dCtl);
  121.             break;
  122.  
  123.         default:
  124.             break;
  125.     }
  126.     return noErr;
  127. }
  128.  
  129. /*------------------------------------------------------*/
  130. /*    Close.                                                */
  131. /*------------------------------------------------------*/
  132.  
  133. pascal short DRVRClose(char *ctlPB, DCtlPtr dCtl)
  134. {                        /* Save & Restore current grafPort? */
  135.     #pragma    unused (ctlPB)
  136.     WindowPtr window;
  137.  
  138.     window = dCtl->dCtlWindow;
  139.     if ( window != nil) {
  140.         dCtl->dCtlWindow = nil;
  141.         DisposeWindow(window);
  142.     }
  143.     return noErr;
  144. }
  145.  
  146. static void drawWindow(WindowPtr window)
  147. {
  148.     int            x, y;
  149.     Rect        myRect;
  150.     GrafPtr        myPort;
  151.     CGrafPtr    myCPort;
  152.     CTabHandle    myctHandle;
  153.     int            myValue;
  154.     RGBColor    myColor;
  155.     ColorSpecPtr    myspecarray;
  156.  
  157.     if (window == nil)
  158.         return;
  159.  
  160.     myColor.red = myColor.green = myColor.blue = 0;
  161.     RGBForeColor(&myColor);
  162.     for (x=1; x< 16; x++) {
  163.         MoveTo(x*16-1, 0);
  164.         LineTo(x*16-1, 255);
  165.     }
  166.     for (y=1; y< 16; y++) {
  167.         MoveTo(0, y*16-1);
  168.         LineTo(255, y*16-1);
  169.     }
  170.  
  171.     GetPort(&myPort);
  172.     myCPort = (CGrafPtr) myPort;
  173.     myctHandle = (**(*myCPort).portPixMap).pmTable;
  174.     
  175.     myspecarray = ((**myctHandle).ctTable);
  176.  
  177.     for (x=0; x< 16; x++) {
  178.         for (y=0; y<16; y++) {
  179.             PmForeColor(y*16+x);
  180.             SetRect(&myRect, x*16, y*16, 15+x*16, 15+y*16);
  181.             PaintRect(&myRect);
  182.             MoveTo(x*16, y*16);
  183.             myColor = myspecarray[y*16+x].rgb;
  184.             myValue = myspecarray[y*16+x].value;
  185.             InvertColor(&myColor);
  186.             RGBForeColor(&myColor);
  187.             Move(2,11);
  188.             if (myValue & (16384)) { /* reserve bit */
  189.                 Line(0,-6);
  190.                 Line(3,0);
  191.                 Line(0,3);
  192.                 Line(-3,0);
  193.                 Line(3,3);
  194.                 Move(3,0);
  195.             } else {
  196.                 Move(6,0);
  197.             }
  198.             if (myValue & (32768)) { /* protect bit */
  199.                 Line(0,-6);
  200.                 Line(3,0);
  201.                 Line(0,3);
  202.                 Line(-3,0);
  203.             }
  204.         }
  205.     }
  206. }
  207.  
  208. static void doCtlEvent(register EventRecord *theEvent)
  209. {
  210.     register    WindowPtr    myWindow;
  211.  
  212.     if (theEvent->what == updateEvt) {
  213.         myWindow = (WindowPtr) theEvent->message;
  214.         SetPort(myWindow);
  215.         BeginUpdate(myWindow);
  216.         drawWindow(myWindow);
  217.         EndUpdate(myWindow);
  218.     }
  219. }
  220.  
  221. static void doPeriodic(DCtlPtr dCtl)
  222. {
  223.     SetPort(dCtl->dCtlWindow);
  224.     drawWindow(dCtl->dCtlWindow);
  225. }